package main

func main() {

}

//#link: https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/
type TreeNode struct {
	Val   int
	Left  *TreeNode
	Right *TreeNode
}

func kthSmallest(root *TreeNode, k int) int {
	var dfs func(root *TreeNode)

	ans := 0
	dfs = func(root *TreeNode) {
		if root != nil && k > 0 {
			dfs(root.Left)
			k--
			if k == 0 {
				ans = root.Val
			}
			dfs(root.Right)
		}
	}
	dfs(root)
	return ans
}
